package com.gsoe.judge.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gsoe.base.exception.JudgeException;
import com.gsoe.base.exception.ProblemException;
import com.gsoe.base.model.Result;
import com.gsoe.base.model.User;
import com.gsoe.base.utils.UserUtil;
import com.gsoe.dto.SaveCacheDTO;
import com.gsoe.judge.client.ProblemClient;
import com.gsoe.judge.mapper.TestCacheMapper;

import com.gsoe.judge.service.TestCacheService;
import com.gsoe.po.ExamCache;
import com.gsoe.po.TestCache;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 测验记录缓存 服务实现类
 * </p>
 *
 * @author xiaoma
 * @since 2024-11-04
 */
@Service
public class TestCacheServiceImpl extends ServiceImpl<TestCacheMapper, TestCache> implements TestCacheService {
    @Autowired
    ProblemClient problemClient;


    @Override
    public boolean saveInput(SaveCacheDTO saveCacheDTO) {
        User loginUser = UserUtil.getLoginUser();
        Result<Long> textRemainingTime;
        try {
            textRemainingTime= problemClient.getTextRemainingTime(saveCacheDTO.getId());
            if(textRemainingTime.getCode() != 200){
                throw new RuntimeException(textRemainingTime.getMsg());
            }
        }catch (Exception e){
            throw new ProblemException(e.getMessage());
        }


        if(textRemainingTime.getData()<=0){
            return false;
        }

        TestCache testCache = lambdaQuery()
                .eq(TestCache::getTestId, saveCacheDTO.getId())
                .eq(TestCache::getUserId, loginUser.getId())
                .eq(TestCache::getProblemId,saveCacheDTO.getProblemId())
                .one();
        if(testCache==null){
            testCache=new TestCache();
            testCache.setUserId(loginUser.getId());
            testCache.setTestId(saveCacheDTO.getId());
            testCache.setProblemId(saveCacheDTO.getProblemId());
        }
        testCache.setInput(saveCacheDTO.getInput());
        saveOrUpdate(testCache);
        return true;
    }

    @Override
    public Map<Long, String> getTestCache(Long testId) {
        if(testId==null){
            throw new JudgeException("getTestCache:测验ID不能为空");
        }
        List<TestCache> testCacheList = lambdaQuery()
                .eq(TestCache::getTestId, testId)
                .list();
        if(CollectionUtil.isEmpty(testCacheList)){return null;}

        return testCacheList.stream().collect(Collectors.toMap(
                TestCache::getProblemId,
                TestCache::getInput
        ));


    }

    @Override
    public void delByTextId(Long id) {
        remove(new QueryWrapper<TestCache>()
                .eq("test_id", id)
        );
    }
}
